<template>
	<el-dialog  :title="title" :visible="showDialog"  @update:visible="updateShowDialog"  @close="beforeClose" >
	    <el-form :model="userData" :rules="userRules" :ref="diyuserRef">
	
            <el-form-item prop="name" label="公司名称">
                <el-input clearable v-model="userData.name" placeholder="请输入公司名称" />
            </el-form-item>
            


			<el-form-item prop="status" label="状态">
			
					<el-select v-model="userData.status" placeholder="请选择">
						<el-option
						v-for="item in status"
						:key="item.value"
						:label="item.label"
						:value="item.value">
						</el-option>
					</el-select>
			
				
            </el-form-item>
				
		
			
            <!-- <el-form-item prop="status" label="状态">
                    <el-select  v-model="userData.status" placeholder="请选择">
                        <el-option v-for="(item,index) in status" :key="index" :label="item.label" :value="item.value"></el-option>
                    </el-select>
            </el-form-item> -->
        </el-form>
	    <div slot="footer" class="dialog-footer">
	      <el-button @click="dCancel($event)">取 消</el-button>
	      <el-button type="primary" @click="dSure($event)">确 定</el-button>
	    </div>
	</el-dialog>
</template>

<script>

export default {

  components: {
	  
  },
  props:{
	  title:String,
	  showDialog:Boolean,
	  diyuserRef:String,
	  userData:Object,
	  userRules:Object
  },
  data() {
    return {
        status:[
				{
					value:0,
					label:'禁用',
				},
				{
					value:1,
					label:'正常',
				}]
    };
  },
  created() {
  },
  mounted() {
  },
  computed: {
  },
  methods: {
	beforeClose(){ // 右上角×号操作
		this.$emit("dCancel","")
	},
	updateShowDialog(value) { //关闭
		let diyuserRef = this.diyuserRef
		this.$refs[diyuserRef].resetFields();
		this.$emit('update:showDialog', value);
	},
	dCancel(){ //取消
		let diyuserRef = this.diyuserRef
		this.$refs[diyuserRef].resetFields();
		if(this.title == "添加公司"){
			console.log('取消')
			this.$emit("dCancel","add")
		}else{
			this.$emit("dCancel","updata")
		}
	},

	dSure(e){ // 确定
		console.log(84,this.title)
		if(this.title == "添加公司"){
			console.log('确认',e)
			this.adduser()
		}else{
			console.log(88)
			this.updatauser()
		}
		
	},
	updatauser(){ //修改用户
		let that = this;
		let diyuserRef = this.diyuserRef
		that.$refs[diyuserRef].validate( async (valid) => {
		  if (valid) {
			let userData = that.userData 
			console.log(277,userData)
			let id = userData.id
			let res = await that.$axios.post( `/api/playlet/company/update/${id}`,userData)
			console.log(334,res)
			that.$message({
				message: '修改成功',
				type: 'success',
				duration: 1200
			})
			that.$refs[diyuserRef].resetFields();
			that.dCancel()
			that.$emit("getData") //调用父组件获取新数据
		  } else {
			that.$message({
				message: '表单填写错误',
				type: 'error',
				duration: 1200
			})
		  }
		});
	},
	adduser(){ //添加用户
		let that = this;
		let userData = that.userData
		let diyuserRef = this.diyuserRef
		that.$refs[diyuserRef].validate( async (valid) => {
		  if (valid) {
			  console.log(128,userData)
                let res = await that.$axios.post( `/api/playlet/company/create`,userData)
                that.$message({
                    message: '添加成功',
                    type: 'success',
                    duration: 1200
                })
                let diyuserRef = that.diyuserRef
                that.dCancel()
                this.$refs[diyuserRef].resetFields();
                this.$emit("getData") //调用父组件获取新数据
		
		  } else {
			that.$message({
				message: '表单填写错误',
				type: 'error',
				duration: 1200
			})
		  }
		});
	}
	
  },
};
</script>

<style >
	/* .el-input__suffix{
		height: 150%;
	} */
	.el-select>.el-input{
		display: inline-block;
	}
</style>